package com.ruoyi.framework.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;

import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitConfig {
    public static final String QUEUE_NAME = "wxQueue";
    public static final String EXCHANGE_NAME = "wxExchange";
    public static final String ROUTING_KEY = "wxRoutingKey";
    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualhost;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualhost);
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        template.setReplyTimeout(12000);
        template.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack) {
                // 消息成功确认
                System.out.println("Message sent successfully: " + correlationData.getId());
            } else {
                // 消息发送失败
                System.out.println("Message sending failed: " + cause);
            }
        });
        // 设置返回回调
        template.setReturnsCallback(returnedMessage -> {
            // 处理无法路由的消息
            System.out.println("Returned message: " + new String(returnedMessage.getMessage().getBody()));
            System.out.println("Reply code: " + returnedMessage.getReplyCode());
            System.out.println("Reply text: " + returnedMessage.getReplyText());
            System.out.println("Exchange: " + returnedMessage.getExchange());
            System.out.println("Routing key: " + returnedMessage.getRoutingKey());
        });
        template.setMandatory(true);
        return template;
    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Queue wxQueue() {
        return new Queue(QUEUE_NAME, true,false,false);
    }

    @Bean
    public TopicExchange wxExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue wxQueue, TopicExchange wxExchange) {
        return BindingBuilder.bind(wxQueue).to(wxExchange).with(ROUTING_KEY);
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
}

